<template>
	<view style="width:100%;height:auto;background:#f5f5f5">
	  <view style="width:100%;height:5rpx"></view>
	  <view class="warp">
		<view v-if="storeInfo.mlogo" class="dplogo">
		  <image :src="uploadFilePath+storeInfo.mlogo"></image>
		</view>
		<view v-else class="dplogo">
		  <image :src="logo"></image>
		</view>
		<view>
			<view style="display: flex;">
				<view style="font-size:1.2rem;font-weight:bold">{{storeInfo.shopName}} </view>
			</view>
		</view>
	  </view>
	  <view class="search-input">
	  	<icon type="search" size="15" style="padding-top:15rpx"/>
	  	<input class="search" @confirm="searchData" type="text" v-model="dname" confirm-type="search" placeholder="请输入商品名" />
	  	<icon type="clear" size="15" @click="clearSearch" style="width:30rpx;padding-top:15rpx;flex:1;text-align: right;"/>
	  </view>
	    <view style="margin-bottom:200upx">
			<block v-for="(data,index) in list"  :v-key="index">
			   <view class="shop">
					<view style="display:flex;justify-content: space-between;align-items: center;"> 
						<view style="display:flex" @click="goGoodsDe(data)">
							<view style="width:200upx">
								<image v-if="data.dimg" mode="aspectFill" style="width:100%;height:4rem;border-radius: 20rpx;"  :src="uploadFilePath+data.dimg"></image>
								<image v-else mode="aspectFill" style="width:100%;height:4rem;"  :src="defgoods"></image>
							</view>
							<view style="margin-left:10upx">
								<view class="box">{{data.dname}}</view>
								<view style="margin-top:0.5rem;font-weight:bold;">
									￥{{data.dprice}}
								</view>
							</view>
						</view>
						<view style="line-height:60upx;">
							<u-tag v-if= "data.isCollect=='1'"  size="mini" borderColor="#999" bgColor="#999"   text="取消收藏" @click="addCollect(data,0)"></u-tag>
							<u-tag v-else  size="mini" type="error" text="添加收藏"  @click="addCollect(data,1)"></u-tag>
							<view style="margin-top:20upx" v-if="data.restShopDisheSpecs.length>0">
								<u-tag  text="选择规格" size="mini" type="warning" @click="changeSpec(data)"></u-tag>
							</view>
						   <view v-else style="margin-top:20upx">
								<u-tag  v-if="data.pickupFlag=='1'"  text="立即购买" size="mini" type="success" @click="addMemo(data,2)"></u-tag>
							   <u-tag v-else text="加购物车" type="primary" size="mini"  @click="addMemo(data,1)"></u-tag>
						   </view>
						</view>
					</view>
				</view>
			  </block>
		  </view>
		  <view class="jsdiv" v-if="isShow && jbnum>0">
		  	<view style="display: flex;padding-left:20upx;align-items: center;">
		  		<view style="position: relative;width:40px;height:40px" @click="goCart(3)">
					<view class="jbcss">
						<u-badge type="error" numberType="limit" shape="circle" max="99" :value="jbnum" ></u-badge>
					</view>
					<image :src="bcart" style="width:80upx;height:80upx;position:absolute;z-index:1"></image>
				</view>
		  		
		  		<view style="margin-left:20upx;margin-left:20upx">
		  			<view>合计 <text style="font-size:16px;font-weight: bold;">￥{{sumMony}}</text></view>
		  			<!-- <view>优惠￥5</view> -->
		  		</view>
		  	</view>
		  	<view class="jxbtn" @click="chosePayWay">去结算</view>
		  </view>
		  <u-popup :show="ggpopup" :round="10" mode="center" closeOnClickOverlay  :customStyle="cusPos"
		   @open="openTset" @close="ggpopup = false">
		  	<view style="padding:20upx">
		  		<view>{{rowData.dname}}</view>
				<view v-if="specData.length>0" style="margin-top:20upx">
					<view v-for="(item,index) in specData" style="display:flex;margin-top:20upx" >
						<view>{{item.speType}}:</view>
						<view v-for="(it,idx) in item.detail" style="display:flex;padding-left:10upx;">
							<u-button :color="`${it.speId == item.checkVal?'#f9ae3d':'#666'}`" size="mini" 
							:text="it.speTypeItem+it.spePrice" 
							style="padding-left:10upx;color:#000000" 
							@click="choseSpec(idx,it.speId,item)"></u-button>
						</view>
					</view>
					<view style="display:flex;align-items:center;margin:20upx;">
						<text>已选规格：</text>
						<u-tag v-for="(ia,ixs) in checkData"
						  :text="ia"
						  type="warning"
						  plain
						  size="mini"
						  style="width: 100upx;"
						></u-tag>
					<!-- 	<span style="font-size:0.8rem">{{checkSpec}}</span> -->
						</view>
				</view>
				<view style="margin-top:20upx">
					<view class="gmsl"><view style="color:#999;font-size: 14px;">购买数量：</view><u-number-box v-model="buyNum" @change="valChange"></u-number-box></view>
					 <sl-filter-view @confirm="getMome"></sl-filter-view>
					 <u--textarea v-model="memo" placeholder="请输入口味偏好等要求" count ></u--textarea>
				</view>
				<view v-if="doPaytype==2">
					<view style="margin:10px 0px">选择支付方式:</view>
					<radio-group @change="radioChange">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in payWayxcx" :key="item.value">
							<view style="display:flex;justify-items: center;">
								<img :src="item.imgurl" style="width:60upx;height:60upx"/>
								<view style="line-height:60upx;height:60upx;padding-left:5px">{{item.label}}</view>
							</view>
							<view>
								<radio :value="item.value" :checked="item.state" />
							</view>
						</label>
					</radio-group>	
				</view>
				<u-button v-if="rowData.pickupFlag == 1" text="购买" size="small" type="success" @click="addOrderAndBuy"></u-button>
		  		<u-button v-else type="success" text="加入购物车" size="small" @click="addCart(null,4)" style="margin-top:20upx"></u-button>
			</view>
		  </u-popup>
		  <u-popup :show="cartpopup" :round="10" mode="center" closeOnClickOverlay :customStyle="cusPos" @open="openTest" @close="cartpopup = false">
			  <view style="width:auto;height:500px;overflow-y: auto">
				  <view style="display: flex;justify-content: space-between;padding:20upx" v-if="cartData.length>0">
					  <view>已选件数（{{cartData.length}}）</view>
					  <view @click="clearCart">
						  <view style="display: flex;"><u-icon name="trash" size="25"></u-icon><text>清空全部</text></view></span清空全部<span></view>
				  </view>
				  <view style="display:flex;justify-content: space-between;padding:20upx;align-items: center;" v-for="(it,idx) in cartData">
					<view style="display:flex;">
						<image mode="aspectFit" v-if="it.restShopDishes.dimg" style="width:3rem;height:4rem;"  :src="uploadFilePath+it.restShopDishes.dimg"></image>
						<image mode="aspectFit" v-else style="width:3rem;height:4rem;"  :src="goodlog"></image>
						<view style="margin-left:20upx">
							<view style="font-weight:bold">{{it.restShopDishes.dname}}</view>
							<view style="margin-top:0.5rem;font-weight:bold">
								￥{{it.restShopDishes.dprice}}
							</view>
							<view>{{it.specsDesc}}</view>
						</view>
					</view>
					<u-number-box 
					   v-model = "it.buyNum"
					>
						 <view slot="minus" class="minus" @click="editCart(it,2)">
							<u-icon
								name="minus-circle"
								size="25"
							></u-icon>
						</view>
						<text
							slot="input"
							style="width: 50px;text-align: center;"
							class="input"
						>{{it.buyNum}}</text>
						<view slot="plus" class="plus" @click="editCart(it,1)">
							<u-icon
								name="plus-circle-fill"
								color="#04743a"
								size="25"
							></u-icon>
						</view>
					</u-number-box>
				  </view>
			  </view>
		  </u-popup>
		  <u-popup :show="showSkuPopup" :round="true" mode="bottom" borderRadius="12">
			<view style="text-align: center;">
				实付金额
			</view>
			<view style="text-align: center;font-size:1.2rem">￥{{sumMony}}</view>
			<view style="margin:10px 0px">选择支付方式:</view>
			<radio-group @change="radioChange">
				<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in payWayxcx" :key="item.value">
					<view style="display:flex;justify-items: center;">
						<img :src="item.imgurl" style="width:60upx;height:60upx"/>
						<view style="line-height:60upx;height:60upx;padding-left:5px">{{item.label}}</view>
					</view>
					<view>
						<radio :value="item.value" :checked="item.state" />
					</view>
				</label>
			</radio-group>					
		  	 <view style="margin:20rpx;display: flex;">
		  	   <u-button @click="onClose" size="small" style="flex:1;text-align: right;">取消</u-button>
		  	   <u-button @click="goCart(2)" color="#F66B71" size="small" style="margin-left:20rpx;flex:1;text-align: right;">确定</u-button>
		  	 </view>
		  </u-popup>
		  <u-modal :show="delState" :title="dtitle" @confirm="removeCart" @cancel="closeMd" :content='content' style="border:1px solid #000000" showConfirmButton showCancelButton></u-modal>
	  </view>
</template>

<script>
	import Config from '@/util/index.js'
	import {
	 queryShopList,queryShopOne
	} from '@/api/goods'
	import {
	 saveCollect,delCollect,cancelCollect
	} from '@/api/collect'
	import {
	 saveCart,cartList,delCart,redayxcxPay,updataCart,cartListOdr,cardxcxPay
	} from '@/api/cart'
	import {
	 getwxOpenId
	} from '@/api/user'
	import slFilterView from '../../../components/sl-filter/filter-view.vue';
	// import cActive from '../../../components/active.vue';
	export default {
		components: {
			slFilterView
		},
		data() {
			return {
				 mid:'',
				 memo:'',
				 osize:0,
				 close3:true,
				 cusPos:{
					 width:'96%'
				 },
				 content:'是否清空购物车',
				 dtitle:'清空购物车',
				 doPaytype:0,
				 cartpopup:false,
				  pagenum:0,
				  jbnum:0,
				  pagesize:10,
				  total:0,
				  sumMony:0,
				  dname:'',
				  goodlog:'',
				  userId:'',
				  defgoods:'',
				  goods:{},
				  timer:'',
				  	rowData:{},
				  moreData:false,
				  noData:false,
				  noActive:false,
				  ggpopup:false,
				  showPay:false,
				  uploadFilePath:'',
				  logo:'',
				  bcart:'',
				  seleMemo:'',
				  payWay:0,
				  buyNum:1,
				  delState:false,
				  list:[],
				  specData:[],
				  isShow:true,
				  memoList:['','微辣','少盐','不要辣椒','不要香菜'],
				  showSkuPopup:false,
				  noticeNull:false,
				  notices:{},
				  actives:[],
				  cartData:[],
				  checkData:[],
				  checkIdData:[],
				  checkSpec:'',
				  loginId:'',
				  rcode:'',
				  scode:'',
				  payWayxcx:[
				  	{
				  	  imgurl:"../../../static/images/wxlog.jpeg",
				  	  value:'0',
				  	  label:'微信支付',
				  	  state:true
				  	},
				  	{
				  	  imgurl:"../../../static/images/cardlog.png",
				  	  value:'1',
				  	  label:'卡支付',
				  	  state:false
				  	}
				  ],
				  storeInfo:{}
			}
		},
		onLoad: function (options) {
			let _this = this
			uni.getStorage({
				key: 'deptdas',
				success: function (res) {
					let depts = JSON.parse(res.data)
					_this.scode = depts.scode
					_this.rcode = depts.rcode			
				}
			})
			// #ifdef MP-WEIXIN
				this.osize = 1
			// #endif
			this.goodlog = this.picUrl + 'goods.png'
			this.logo =  this.picUrl + 'logo.png'
			this.defgoods = this.picUrl + 'goods.png'
			this.bcart = this.picUrl + 'bcart.png'
			this.userId = uni.getStorageSync("userId")
		    this.mid = options.mid
			console.log("this.mid",this.mid)
			this.uploadFilePath = Config.get('apiUrl')
		    this.getSGoodsList()
			this.goCart(1)
		},
		beforeDestroy() { 
			clearInterval(this.timer);
		},
		methods: {
			clearCart(){
				this.cartpopup = false
				this.delState = true
			},
			chosePayWay(){
				this.showSkuPopup = true
			},
			onClose(){
				this.showSkuPopup = false
			},
			valChange(e) {
				console.log('当前值为: ' + e.value)
			},
			closeMd(){
				this.delState = false
				this.cartpopup = true
			},
			removeCdate(idx,num){
				this.close3 = false
				this.specData.forEach((it,ix)=>{
					it.detail.forEach(its=>{
						if(its.speTypeItem == idx){
							let ix = this.checkData.indexOf(its.speTypeItem)
							this.checkData.splice(ix,1)
							it.checkVal = ""
						}
					})
				})
				console.log("this.specData",this.specData)
				console.log("checkData1",this.checkData)
			},
			removeCart(){
				let arr = []
				this.cartData.forEach(item=>{
					arr.push(item.ocartId)
				})
				let params = arr.toString()
				delCart(params).catch(res => {
					if(res.data.code == 200){
						uni.showToast({
						  title:"删除成功",
						  icon: 'none',
						  duration: 2000
						});
						this.delState = false
						this.pageNum = 1
						this.sumMony = 0
						this.jbnum = 0
						this.cartData=[]
						this.cartpopup = false
						this.goCart(1)
					}
				})
			},
			clearSearch(){
				this.dname = ""
				this.pagenum = 0
				this.list = []
				this.getSGoodsList()
			},
			getMome(res){
				let result = res.result.jobType
				this.seleMemo=""
				if(result.length>0){
					result.map(item =>{
						this.seleMemo+=this.memoList[item]+','
					})
				}
			},
			radioChange(val){
				this.payWay = val.detail.value
			},
			openTest(){},
			goCart(num){
				//num 1:加入购物车图标，2：去结算按钮， 3：购物车图标 4:选规格加购
				let pas = {
					userId:this.userId,
					shopId:this.mid
				}
				let math = require('mathjs');
				cartList(pas).catch(resx => {
					if(resx.data.cart){
						resx.data.cart.forEach(item=>{
							if(item.shopId==this.mid){
								this.jbnum = item.disheorderCarts.length
								if(num==1 || num==4){
									item.disheorderCarts.forEach(itm=>{
										let stoal = 0
										let gprice = 0
										let namss = []
										let math = require('mathjs');
										if(itm.specsDesc){									
											let arr = itm.specsDesc.split(";")
											for(let i=0;i<arr.length;i++){
												let arr2 = arr[i].split(',')
												let dx = arr2[0].indexOf(":")
												let nax = arr2[0].substr(dx+1)
												if(nax){
													namss.push(nax)
												}
												if(arr2[arr2.length-1]){
													stoal = math.add(stoal,arr2[arr2.length-1])
												}
											}
											let addspcpric = math.add(stoal,itm.dprice)
											gprice = math.multiply(addspcpric,itm.buyNum)
										}else{
											gprice = math.multiply(itm.dprice,itm.buyNum)
										}
										this.sumMony = math.add(this.sumMony,gprice) 
									})
									this.cartData = item.disheorderCarts
									console.log("this.cartData",this.cartData)
								}else if(num==2){
									this.cartData = item.disheorderCarts
									this.addCartSave()
								}else{
									//结算
									this.cartData = item.disheorderCarts
								}
							}
						})
						this.sumMony = math.round(this.sumMony,2)
					}else{
						this.sumMony = 0
					}
				})
				if(num==3){
					this.cartpopup = true
				}
			},
			openTset(){},
			//直接结算，不走购物车
			addOrderAndBuy(row){
				if(!row){
					row = this.rowData
				}
				if(row.dprice == undefined){
					return  uni.showToast({
						title:"商品未定价，不能购买",
						icon: 'none',
						duration: 2000
					});
				}
				let params = {
					orderId:"",
					userId:this.userId,
					ostate:0,
					lstate:'0',								
					shopId:row.shopId,
					endTime:'',
					paymoney:0,//实付金额
					originalPrice:row.dprice,//原价
					ltime:"",
					pickupFlag:1,
					disheorderOrderitems:[{
						itemId:'',
						ordId:'',
						dishesId:row.dishesId,
						speName:'',
						buyNum:this.buyNum,
						memo:this.seleMemo+this.memo,
						idisPrice:row.dprice,//基础价格
						addPrice:'',//规格加价
						ipayNum:0,//实付金额
						isflag:1
					}]
				}
				let stoal = 0
				let namss = []
				let math = require('mathjs');
				if(row.specsDesc){									
					let arr = row.specsDesc.split(";")
					for(let i=0;i<arr.length;i++){
						let arr2 = arr[i].split(',')
						let dx = arr2[0].indexOf(":")
						let nax = arr2[0].substr(dx+1)
						if(nax){
							namss.push(nax)
						}
						if(arr2[arr2.length-1]){
							stoal = math.add(stoal,math.bignumber(arr2[arr2.length-1]))
						}
					}
					params.disheorderOrderitems[0].speName = namss.toString()
				}
				let gmoney = math.multiply(this.buyNum,row.dprice)
				params.disheorderOrderitems[0].addPrice = stoal
				params.disheorderOrderitems[0].ipayNum = math.add(stoal,gmoney)
				params.paymoney = math.add(stoal,gmoney)
				cartListOdr(params).catch(resp=>{
					this.orderId = resp.data.data
					let _this = this
					// #ifdef MP-WEIXIN
						if(_this.payWay==1){
							//卡支付
							let psx = {
								userId:this.userId,
								orderId:this.orderId
							}
							cardxcxPay(psx).catch(respz=>{	
								_this.showSkuPopup = false
								if(respz.data.code==200){
									_this.ggpopup = false
									uni.navigateTo({
										 url:'../my/order/index'
									})
								}
							})
						}else{
							uni.login({
							  provider: 'weixin',
							  success: res => {
								if (res.code) {
								  // 获取到登录凭证code
								  let code = res.code;
								  // 调用后端接口，传递code进行获取openid操作
								  console.log("code",res.code)
								  _this.getOpenid(code)
								}
							  }
							});
						}
					// #endif
					// #ifndef MP-WEIXIN
						this.showPay = false
					// #endif
				})
			},
			addCartSave(){			
				let math = require('mathjs');
				let params = {
					orderId:"",
					userId:this.userId,
					ostate:0,
					lstate:'0',	
					shopId:this.mid,
					endTime:'',
					paymoney:0,//实付金额
					originalPrice:0,//原价
					ltime:"",
					memo:this.seleMemo+this.memo,
					pickupFlag:0,
					disheorderOrderitems:[]
				}
				var promise = new Promise((resolve2, reject) => {
					this.cartData.forEach(its=>{
						let obj = {
							itemId:'',
							ordId:'',
							dishesId:its.dishesId,
							buyNum:its.buyNum,
							memo:'',
							idisPrice:its.dprice,
							addPrice:0,
							ipayNum:its.dprice*its.buyNum,
							isflag:1
						}
						let stoal = 0
						let ptoal = 0
						let namss = []
						params.originalPrice += math.multiply(its.dprice,its.buyNum)
						if(its.specsDesc){
							let arr = its.specsDesc.split(";")
							for(let i=0;i<arr.length;i++){
								let arr2 = arr[i].split(',')
								let dx = arr2[0].indexOf(":")
								let nax = arr2[0].substr(dx+1)
								if(nax){
									namss.push(nax)
								}
								if(arr2[arr2.length-1]){
									stoal = math.add(stoal,arr2[arr2.length-1])
								}
							}
							obj.speId = its.speIds
							obj.speName = namss.toString()
						}				
						params.paymoney = this.sumMony									
						params.disheorderOrderitems.push(obj)
					})
					resolve2(params);
				  })
				  promise.then(res => {		
					let _this = this
					cartListOdr(res).catch(resp=>{
						this.orderId = resp.data.data
						// #ifdef MP-WEIXIN
							uni.login({
							  provider: 'weixin',
							  success: res => {
								if (res.code) {
								  // 获取到登录凭证code
								  let code = res.code;
								  // 调用后端接口，传递code进行获取openid操作
								  console.log("code",res.code)
								  if(_this.payWay==1){
									 //卡支付
									 let psx = {
									 	userId:this.userId,
									 	orderId:this.orderId
									 }
									 cardxcxPay(psx).catch(respz=>{	
										this.showSkuPopup = false
									 	if(respz.data.code==200){
									 		uni.navigateTo({
									 			 url:'../my/order/index'
									 		})
									 	}
									 })
								  }else{
									uni.login({
									  	provider: 'weixin',
									  	success: res => {
									  		if (res.code) {
									  			// 获取到登录凭证code
									  			let code = res.code;
									  			// 调用后端接口，传递code进行获取openid操作
												_this.getOpenid(code)  
									  		}
									  	}
									});
								  }
								}
							  }
							});
						// #endif
						// #ifndef MP-WEIXIN
							this.showPay = false
						// #endif
					})				
				})
			},
			getOpenid(cdos){
				let params = {
					code:cdos,
					userId:this.userId
				}
				
				getwxOpenId(params).catch(resp=>{
					if(resp.data){
						let ams = {
							orderNumber:this.orderId,
							openId:resp.data.openid
						}
						console.log("ams",ams)
						redayxcxPay(ams).catch(resp=>{
							let _this = this
							  //调用微信官方支付接口弹出付款界面,输入密码扣款
							wx.requestPayment({
							     timeStamp:resp.data.timeStamp,  //时间戳
							     nonceStr:resp.data.nonceStr,   //随机字符串
							     package:resp.data.package,    //prepay_id
							     signType:resp.data.signType,   //签名算法MD5
							     paySign:resp.data.paySign,    //签名
							     success (res) {
							       if (res.errMsg == "requestPayment:ok"){
							          console.log('支付成功', res)
									  uni.showToast({
									    title:"支付成功",
									    icon: 'none',
									    duration: 2000
									  });
									  _this.showPay = false
									  _this.ggpopup = false
									  _this.goCart(1)
							       }else{
									  uni.showToast({
									    title:"支付失败",
									    icon: 'none',
									    duration: 2000
									  });
							       }
								   uni.redirectTo({
								   	 url:'../my/order/index'
								   })
							     },
							     fail (res) { 
								   uni.showToast({
								     title:"已取消支付",
								     icon: 'none',
								     duration: 2000
								   });
								   _this.showSkuPopup = false
								   _this.goCart(1)
								   let pas = {
								   	userId:_this.userId,
								   	shopId:_this.mid
								   }
								   cartList(pas).catch(resx => {
									   _this.cartData = item.disheorderCarts
								   })
							    }
							})
						})
					}
				})
			},
			changeSpec(row){
				this.rowData = row
				// this.specData = row.restShopDisheSpecs
				const groupBy = (array, key) => {
				  return array.reduce((result, currentItem) => {
					// 使用 key 的值作为分组的键
					const groupKey = currentItem[key];
					if (!result[groupKey]) {
					  result[groupKey] = [];
					}
					// 将当前项添加到对应的分组中
					let obj = {
					  speId:currentItem.speId,
					  // speType:currentItem.speType,
					  speTypeItem:currentItem.speTypeItem,
					  spePrice:currentItem.spePrice,
					  isflag:parseInt(currentItem.isflag)
					}
					result[groupKey].push(obj);
					return result;
				  }, {});
				};
				if(this.rowData.pickupFlag==1){
					this.doPaytype = 2//直接买
				}else{
					this.doPaytype = 1//加购
				}
				let spcdata = row.restShopDisheSpecs
				let tdata = groupBy(spcdata, 'speType');
				let names = Object.keys(tdata)
				this.specData=[]
				for(let i=0;i<names.length;i++){
				  let xx = {
					dishesId:row.dishesId,
					speType:names[i],
					checkVal:"",
					detail:tdata[names[i]]
				  }        
				  this.specData.push(xx)
				}
				this.ggpopup = true
			},
			//更新购买数量
			editCart(row,num){
				let math = require('mathjs');
				//添加购物车	
				let numz = 0
				let bnum = 0
				if(num==2){
					numz = math.subtract(this.sumMony,row.dprice)
					bnum = math.subtract(row.buyNum,1)
				}else{
					numz = math.add(this.sumMony,row.dprice)
					bnum = math.add(row.buyNum,1)
				}
				if(bnum>0){
					let params = {
						ocartId:row.ocartId,
						userId:this.userId,
						shopId:row.shopId,
						dishesId:row.dishesId,
						dprice:row.dprice,
						deptId:this.rcode,
						buyNum:bnum,
						speIds:row.restShopDisheSpecs.length==0?"":this.checkIdData.join(','),
						isflag:1
					}
					this.sumMony = math.round(numz,2)
					updataCart(params).catch(ress => {
						if(ress.code == 200){
							uni.showToast({
							  title:"修改成功",
							  icon: 'none',
							  duration: 2000
							});
						}
					})
				}else{
					let orid = row.ocartId
					delCart(orid).catch(res => {
						if(res.data.code == 200){
							uni.showToast({
							  title:"删除成功",
							  icon: 'none',
							  duration: 2000
							});
							this.sumMony = 0
							this.goCart(1)
						}
					})
				}
			},
			addMemo(row,type){
				this.doPaytype = type
				this.rowData = row
				this.specData = []
				this.ggpopup = true
			},
			addCart(row,type){
				this.sumMony = 0
				let _this = this
				//添加购物车
				if(type==4){
					row = this.rowData
				}
				let params = {
					ocartId:"",
					userId:this.userId,
					shopId:row.shopId,
					dishesId:row.dishesId,
					dprice:row.dprice,
					buyNum:this.buyNum,
					deptId:_this.rcode,
					pickupFlag:0,
					memo:this.seleMemo+this.memo,
					speName:row.restShopDisheSpecs?_this.checkData.toString():"",
					speIds:row.restShopDisheSpecs?_this.checkIdData.join(","):"",
					isflag:1
				}
			
				if(row.dprice){
					// let math = require('mathjs');
					// let sumx = math.add(this.sumMony,params.dprice)
					// this.sumMony = math.round(sumx,2)
					saveCart(params).catch(resp => {
						if(resp.data.code == 200){
							uni.showToast({
							  title:"加入购物车成功",
							  icon: 'none',
							  duration: 2000
							});
							_this.ggpopup = false
							_this.isShow = true
							_this.goCart(type)
						}
					})
				}else{
					uni.showToast({
					  title:"该商品未定价，不能售卖",
					  icon: 'none',
					  duration: 2000
					});
				}
				
			},
			choseSpec(cid,id,row){
				this.checkData = []
				this.specData.forEach((item,idx)=>{
					item.detail.forEach(it=>{			
						if(id == it.speId){
							if(row.checkVal == id){
								let ix = this.checkData.indexOf(it.speTypeItem)
								this.checkData.splice(ix,1)
								item.checkVal = ""
							}else{
								item.checkVal = id
								this.checkData[idx] = it.speTypeItem
								this.checkIdData[idx] = it.speId
							}
						}
					})
				})
			},
			searchData(){
				if(this.dname){
					this.pagenum = 0
					this.list = []
					this.getSGoodsList()
				}
			},
			addCollect(row,type){
				if(type==1){
					let params = {
						ocartId:"",
						userId:this.userId,
						shopId:this.mid,
						dishesId:row.dishesId,
						dprice:row.dprice,
						buyNum:1,
						deptId:this.rcode
					}
					let _this = this
					if(row.dprice){
						saveCollect(params).catch(resp => {
							if(resp.data.code == 200){
								uni.showToast({
									title:"收藏成功",
									icon: 'none',
									duration: 2000
								});
							}
						})
					}else{
						uni.showToast({
							title:"该商品未定价，不能收藏",
							icon: 'none',
							duration: 2000
						});
					}
				}else{
					// delCollect(ocid).catch(res => {
					// 	if(res.data.code == 200){
					// 		this.delState = false
					// 		uni.showToast({
					// 			title:"已取消收藏",
					// 			icon: 'none',
					// 			duration: 2000
					// 		});
					// 	}
					// })
					let params = {
						dishesId:row.dishesId,
						loginid:this.userId
					}
					cancelCollect(params).catch(res=> {
						if(res.data.code == 200){
						  uni.showToast({
						  	title:"取消收藏成功",
						  	icon: 'none',
						  	duration: 2000
						  });
						}
					})
				}
				uni.reLaunch({
				  url: `/pages/student/shop/index?mid=${this.mid}`
				})
			},
			getStoreInfo(){
			    let _this = this
				queryShopOne(this.mid).catch(res => {
					console.log("storeInfo",res.data.data)
			     _this.storeInfo=res.data.data
			      // _this.getNoticeInfo()
			    })
			},
			getSGoodsList(){
			    if(this.moreData){
			      return false
			    }else{
			        this.pagenum++
			    }
			    uni.showLoading({
			      title: '加载中...',
			    })
			    let params = {
			        shopId:this.mid,
					deptId:this.rcode,
					userId:this.userId,
			        pageNum:this.pagenum,
					dname:this.dname,
			        pageSize:this.pagesize,
					salesFlag:1
			    }
				console.log("pname",this.pagenum)
				queryShopList(params).catch(res => {
					 if(res.data.rows.length > 0){
						 this.storeInfo.shopName = res.data.rows[0].restUserShop.shopName
						 this.storeInfo.shopAdd = res.data.rows[0].restUserShop.shopAdd
					   const newList = res.data.rows
					   uni.hideLoading()
					   const total = parseInt(res.data.total)
					   if(this.list.length < total){
							this.list = [...this.list,...newList]
					       this.total =total
					   }else{
						   this.moreData = true
					   }
					   
					 }else{
					   uni.hideLoading()
					   if(this.pagenum==0){
						   this.noData = true
					   }
						this.moreData = true
					 }
				})
			},
			  goGoodsDe(data){
			    uni.navigateTo({
			      url: '../goods/detail?gid='+data.dishesId
			    })
			  },
			  onChangenum(event) {
			    console.log(event.detail);
			  },
			onReachBottom(){
			  	this.getSGoodsList()
			},
		}
	}
</script>

<style>
	.warp{display:flex;margin-top:10rpx;background:#ffffff;padding:20rpx;align-items:center}
	.dplogo image{width:100rpx;height:100rpx;padding-right:10rpx;}
	.dpwarp{width:100%;height:auto;background-color: #f5f5f5;}
	.ggcss{padding:20rpx;background-color: #fff;margin:10rpx 0rpx;}
	.shop{padding:20rpx;background:#ffffff;margin-top:10rpx;}
	.jsdiv{
		  width:90%;height:100upx;border-radius: 50px;border:1px solid #f5f5f5;box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);display: flex;justify-content: space-between;
		  margin:20upx 30upx;position: fixed;bottom:0upx;background:#fff
	}
	.search-input {
	  color: #999;
	  padding: 20rpx;
	  background: #f1f1f2;
	  display: flex;
	}
	.search-input .search-icon {
	    margin-right: 8rpx;
	}
	.box {
	     /*强制文本在一行内显示*/
	     white-space: nowrap; 
	     overflow: hidden;
	     text-overflow: ellipsis;
		 font-weight: bold;
	}
	.search{width:85%;}
	.gmsl{display:flex;margin:20upx 0upx;align-items: center;}
	.jbcss{width:50upx;height:auto;text-align: center;margin-left:30px;position:absolute;z-index: 10;}
	.botcart{height:60upx;line-height:60upx;padding:20upx;display:flex;justify-content: space-between;border-bottom:1px solid #f5f5f5}
	.jxbtn{width:150upx;height:100upx;font-size:18px;line-height:100upx;text-align: center;
	background-color:#04743a;color:#fff;border-top-right-radius: 50px;border-bottom-right-radius: 50px;}
	.uni-list-cell{display:flex;justify-items: center;justify-content: space-between;margin:10px 0px}
	.utag{width:auto;height:1rem;border: 1px solid #F66;font-size: 0.8rem;padding:0.2rem;border-radius: 10px;margin-right:0.5rem}
</style>
